package itheima.com.testlistfragmentdemo.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import itheima.com.testlistfragmentdemo.MyApplication;
import itheima.com.testlistfragmentdemo.R;
import itheima.com.testlistfragmentdemo.base.BaseListAdpater;
import itheima.com.testlistfragmentdemo.entity.News;

//创建一个RecyclerView的数据适配器
public  class NewsAdapter extends BaseListAdpater<News>{

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
       View itmeView= LayoutInflater.from(MyApplication.getContext()).inflate(R.layout.item_news,parent,false);

        return new NewsHolder(itmeView);
    }
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        //取得当前列表条目的数据
        final News news = list.get(position);
        NewsHolder newsHolder= (NewsHolder) holder;
        newsHolder.tv.setText(news.getTitle());
        //对列表条目设置点击监听
        newsHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MyApplication.getContext(), news.getTitle(), Toast.LENGTH_SHORT).show();
            }
        });
    }
    private class NewsHolder extends  RecyclerView.ViewHolder{
        TextView tv;

        public NewsHolder(View itemView) {
            super(itemView);
            tv= (TextView) itemView.findViewById(R.id.tv_name);
        }
    }
}